package controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.AppCategory;
import pojo.AppInfo;
import service.infoservice;
import util.Page;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dell on 2019/1/28.
 */
@Controller
@RequestMapping("info")
public class infocontroller {
    @Resource
    public infoservice infoservice;


    @RequestMapping("categoryLevel1")
    @ResponseBody
    public   String categoryLevel1(){

        List<AppCategory> categoryLevel1=new ArrayList<AppCategory>();
        categoryLevel1=
                infoservice.categoryLevel1();

        return JSONArray.toJSONString(categoryLevel1);
    }


    @RequestMapping("infoall")
    @ResponseBody
    public String all(AppInfo appInfo,@RequestParam(value = "pageIndex", required = false) Integer pageIndex) {

        Page page = new Page();
        //条数
        int infoCount = infoservice.Cont();
        //设置页面容量
        int pageSize = 5;
        page.setPageSize(pageSize);
        page.setTotalCount(infoCount);
        int currPageNo = 0;
        if (pageIndex == null) {
            pageIndex = 1;
        }
        currPageNo = (pageIndex - 1) * pageSize;
        page.setCurrPageNo(currPageNo);
        List<AppInfo> list = new ArrayList<AppInfo>();
        list = infoservice.infoAll(appInfo,currPageNo, pageSize);
        HashMap<String, Object> appInfoMap = new HashMap<String, Object>();
        appInfoMap.put("list", list);
        appInfoMap.put("pages", page);
        System.out.print(appInfoMap);
        return JSONArray.toJSONString(appInfoMap);


    }

    @RequestMapping("delect")
    @ResponseBody
    public int delect(@RequestParam(value = "idd", required = false) int idd) {

        int a = infoservice.delect(idd);
        System.out.print(a);
        return a;
    }
    @RequestMapping("update")
    @ResponseBody
    public  int update(@RequestParam(value = "id" ,required = false)int id){
        int b=infoservice.update(id);
        return b;
    }


}
